package com.ll.beautymall.ui.home.zero.model;

import android.util.Log;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.ll.beautymall.bean.ObjectProduct;
import com.ll.beautymall.bean.ZeroData;
import com.ll.beautymall.bean.ZeroHeader;
import com.ll.beautymall.utils.HttpUtils;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.HashMap;
import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

/**
 * Created by Administrator on 2016/10/25.
 */
public class ZeroBiz {
    public void getZeroHeaderData(HashMap<String, String> parameters, final OnGetZeroHeaderLisenter lisenter) {
        HttpUtils.getInstance().getZeroHeader(parameters, new Callback<String>() {
            @Override
            public void onResponse(Call<String> call, Response<String> response) {
                if (response.isSuccessful()) {
                    try {
                        String json = response.body();
                        JSONObject object = new JSONObject(json);
                        JSONObject data = object.getJSONObject("data");
                        Gson gson = new Gson();
                        ZeroHeader zeroHeader = gson.fromJson(data.toString(), new TypeToken<ZeroHeader>() {
                        }.getType());
                        if (zeroHeader != null) {
                            Log.d("TAG", ">>>header>>>" + zeroHeader.getDescription());
                            lisenter.onSucced(zeroHeader);
                        }
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                }
            }

            @Override
            public void onFailure(Call<String> call, Throwable t) {
                lisenter.onFailed("网络连接较慢，请检查网络...");
            }
        });
    }

    public void getZeroData(HashMap<String, String> parameters, final OnGetZeroDataLisenter lisenter) {
        HttpUtils.getInstance().getZeroData(parameters, new Callback<String>() {
            @Override
            public void onResponse(Call<String> call, Response<String> response) {
                if (response.isSuccessful()) {
                    try {
                        String json = response.body();
                        JSONObject object = new JSONObject(json);
                        JSONObject data = object.getJSONObject("data");
                        String next_cursor = data.getString("next_cursor");
                        String previous_cursor = data.getString("previous_cursor");
                        String product_type = data.getString("product_type");
                        int action_type = data.getInt("action_type");
                        HashMap<String, String> scale = new HashMap<String, String>();
                        JSONObject display_scale = data.getJSONObject("display_scale");
                        scale.put("x", display_scale.getString("x"));
                        scale.put("y", display_scale.getString("y"));
                        JSONArray products = data.getJSONArray("products");
                        Gson gson = new Gson();
                        List<ObjectProduct> objectProducts = gson.fromJson(products.toString(), new TypeToken<List<ObjectProduct>>() {
                        }.getType());
                        ZeroData zeroData = new ZeroData(next_cursor, previous_cursor, product_type, action_type, objectProducts, scale);
                        if (zeroData != null) {
                            lisenter.onSucced(zeroData);
                        }
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                }
            }

            @Override
            public void onFailure(Call<String> call, Throwable t) {
                lisenter.onFailed("网络连接较慢，请检查网络...");
            }
        });
    }
}
